package com.yingyi.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.yingyi.domain.Store;
import com.yingyi.service.IStoreService;
import com.yingyi.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/store")
public class StoreController {

    @Autowired
    private IStoreService storeService;


    @SentinelResource(value = "store-server-add",
            blockHandler = "addBloceHandler",
            fallback = "addFallback"
    )
    @PostMapping("/add")
    public JsonResult add(@RequestBody Store store) {
        storeService.add(store);
        //int i = 1 / 0;
        return JsonResult.me();
    }

    public JsonResult addBloceHandler(@RequestBody Store store, BlockException e) {
        e.printStackTrace();
        return JsonResult.me().setMsg("触发限流了，请稍后重试");
    }

    public JsonResult addFallback(@RequestBody Store store, Throwable throwable) {
        throwable.printStackTrace();
        return JsonResult.me().setMsg("触发熔断了，稍后再来吧");
    }

}
